package bdgrade;

import java.awt.EventQueue;

import javax.swing.JFrame;
import java.awt.BorderLayout;
import javax.swing.JPanel;
import java.awt.Color;
import javax.swing.JLabel;
import javax.swing.JTextPane;

import domain.Stu;
import service.StuService;
import service.Impl.StuServiceImpl;
import tools.ReadTool;

import java.awt.Font;
import javax.swing.JButton;
import javax.swing.JFileChooser;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.List;
import javax.swing.ImageIcon;

public class ImportTips {
	//注入service
	StuService stuService=new StuServiceImpl();
	//注入写入文件类ReadTool
	ReadTool readTool=new ReadTool();
	private JFrame frame;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					ImportTips window = new ImportTips();
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public ImportTips() {
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frame = new JFrame();
		frame.setBounds(100, 100, 576, 371);
		frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		frame.getContentPane().setLayout(new BorderLayout(0, 0));
		frame.setVisible(true);
		JPanel panel = new JPanel();
		panel.setBackground(Color.BLACK);
		frame.getContentPane().add(panel, BorderLayout.CENTER);
		panel.setLayout(null);
		
		JTextPane textPane = new JTextPane();
		textPane.setFont(new Font("宋体", Font.PLAIN, 18));
		textPane.setBackground(Color.BLACK);
		textPane.setText("批量导入学生信息,需要按照格式写好学生的信息,否则程序无法识别！\r\n格式(建议直接复制以下范例进行修改):\r\n101,张三,数据结构,大数据1班,90\r\n.......");
		textPane.setForeground(Color.WHITE);
		textPane.setBounds(0, 13, 564, 105);
		panel.add(textPane);
		
		JButton btnNewButton = new JButton("我已写好txt文件,导入");
		btnNewButton.setForeground(Color.WHITE);
		btnNewButton.setBackground(Color.BLACK);
		
		btnNewButton.setFont(new Font("宋体", Font.PLAIN, 15));
		btnNewButton.setBounds(84, 205, 215, 48);
		panel.add(btnNewButton);
		
		JButton btnNewButton_1 = new JButton("返回");
		btnNewButton_1.setForeground(Color.WHITE);
		btnNewButton_1.setBackground(Color.BLACK);
		btnNewButton_1.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				frame.dispose();
			}
		});
		btnNewButton_1.setFont(new Font("宋体", Font.PLAIN, 15));
		btnNewButton_1.setBounds(313, 205, 113, 48);
		panel.add(btnNewButton_1);
		
		JLabel label = new JLabel("");
		label.setIcon(new ImageIcon(ImportTips.class.getResource("/img/6.jpg")));
		label.setBounds(10, 0, 483, 451);
		panel.add(label);
		btnNewButton.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				String path = null;
				JFileChooser fc = new JFileChooser();
				fc.setDialogTitle("请选择要导入的学生信息(txt文件)");
				fc.setApproveButtonText("确定");
				fc.setFileSelectionMode(JFileChooser.FILES_ONLY);
				if (JFileChooser.APPROVE_OPTION == fc.showOpenDialog(panel)) {
				path=fc.getSelectedFile().getPath();
				}
				
				ReadTool rt=new ReadTool();
				rt.setPath(path);
				List<Stu>list=rt.readUsers();
				int ret=0;
				for(Stu s:list) {
					stuService.add(s);
					ret=1;
				}
				if(ret==1) {
					//导入成功
					btnNewButton_1.setBackground(Color.GREEN);				
				}
				else btnNewButton_1.setBackground(Color.RED);					
			}
			
		});
	}
}
